package sfsu.memopad;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

// Custom adapter for handling a list of TaskData.
public class TaskListAdapter extends ArrayAdapter<TaskData>{

    Context context; 
    int layoutResourceId;    
    ArrayList<TaskData> data;
    
    public TaskListAdapter(Context context, int layoutResourceId, ArrayList<TaskData> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        TaskListHolder holder = null;
        
        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            
            holder = new TaskListHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
            
            row.setTag(holder);
        }
        else
        {
            holder = (TaskListHolder)row.getTag();
        }
        
        TaskData taskLineItem = data.get(position);

        holder.txtTitle.setText(taskLineItem.getTitle());	// Sets title to be shown.
        
        // Set icon depending if it is a group or individual task.
        if (taskLineItem.getGroupID() == -1) {
        	holder.imgIcon.setImageResource(R.drawable.ic_menu_edit);
        } else {
        	holder.imgIcon.setImageResource(R.drawable.group);
        }
        
        return row;
    }
    
   static class TaskListHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
    }
}
